
package com.company.project.controller;

import cn.hutool.core.lang.Console;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.webservice.SoapClient;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.company.project.core.Result;
import com.company.project.core.ResultGenerator;
import com.company.project.model.User;
import com.company.project.utils.XmlTool;
import lombok.extern.slf4j.Slf4j;
import org.dom4j.DocumentException;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import org.apache.cxf.endpoint.Client;
import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory;

/**
 * webservice 请求实例
 *
 * @author PengChenXi
 * @since 2022/05/18.
 */
@RestController
@RequestMapping("/ws")
@Slf4j
public class WebServiceController {

    /**
     * 请求ws服务
     *
     * @return
     * @throws DocumentException
     */

    @PostMapping("/request")
    public void requestWebService() {
        String hisApiHost = "http://172.16.16.97:8015/Service1.asmx?wsdl";
        String patientId = "P00001";
        String xml =
                "<Request>\n" +
                        "<TranCode>99001</TranCode>\n" +
                        "<BINGRENID>" + patientId + "</BINGRENID> " +
                        "<HLWBZ>1</HLWBZ> " +
                        "<PSBZ>1</PSBZ> " +
                        "<CHUFANGID>('1111111')</CHUFANGID>\n" +
                        "</Request>";
        log.error("发送HIS药房-快递标记-原始入参 xml:" + xml);
        JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
        Client client = dcf.createClient(hisApiHost);
        // 须要密码的状况须要加上用户名和密码
        // client.getOutInterceptors().add(new ClientLoginInterceptor(USER_NAME, PASS_WORD));
        Object[] objects = new Object[0];
        try {
            // invoke("方法名",参数1,参数2,参数3....);
            objects = client.invoke("Getyinyi_sp", xml);
            log.error("返回数据:" + objects);
            log.error("返回数据:" + objects[0]);

            JSONObject obj = XmlTool.documentToJSONObject((String) objects[0]);
            if (obj.getString("ResultCode").equals("0")) {
                log.error(obj.getString("ErrorMsg"));

            } else {
                log.error(obj.getString("ErrorMsg"));
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
        }
    }


    /**
     * hutool 工具类使用事例
     * 具体参考hutool文档:https://www.hutool.cn/docs/
     *
     * @param user
     * @return
     */
    @PostMapping("/hutool")
    public Result hutool(@RequestBody @Validated User user) {
        //具体参考hutool文档:https://www.hutool.cn/docs/
        System.out.println(user.toString());
        //创建JSON对象
        cn.hutool.json.JSONObject json1 = JSONUtil.createObj()
                .put("a", "value1")
                .put("b", "value2")
                .put("c", "value3");

        //加密算法
        String md5 = SecureUtil.md5("this-is-key");
        //国产SM算法 sm1,sm2,sm3,sm4
        String digestHex = SmUtil.sm3("aaaaa");
        //Restful请求
        String json = "{\"name\":\"peng\"}";
        String url = "http://baidu.com";
        String result2 = HttpRequest.post(url)
                .body(json)
                .execute().body();

        //WebService 请求
        // 新建客户端
        SoapClient client = SoapClient.create("http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx")
                // 设置要请求的方法，此接口方法前缀为web，传入对应的命名空间
                .setMethod("web:getCountryCityByIp", "http://WebXml.com.cn/")
                // 设置参数，此处自动添加方法的前缀：web
                .setParam("theIpAddress", "218.21.240.106");

        // 发送请求，参数true表示返回一个格式化后的XML内容
        // 返回内容为XML字符串，可以配合XmlUtil解析这个响应
        Console.log(client.send(true));
        return ResultGenerator.genSuccessResult();
    }



}
